﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;

namespace delizios
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded +=new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
		
		// page exit 
		private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Do you really want to exit?", "Message", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            { e.Cancel = true; }
        }

		private void showMenu_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			// show menu guide
			showMenu.Content = "Show";
            XElement xml = XElement.Load("menu.xml");

            var items = from item in xml.Descendants("food")
                        select new menu
                        {
                            UserImage = item.Element("picture").Value,
                            Text = item.Element("name").Value,
                            DateTime = item.Element("data").Value
                        };
            lstTwitter.ItemsSource = items;
		}

		private void showMenu_Unchecked(object sender, System.Windows.RoutedEventArgs e)
		{
			// hide menu guide
			showMenu.Content = "Hide";
            lstTwitter.ItemsSource = "";
		}

		private void food_Tap(object sender, System.Windows.Input.GestureEventArgs e)
		{
			// open picture page
			NavigationService.Navigate(new Uri("/picturePage.xaml", UriKind.RelativeOrAbsolute));
		}

		private void about_Tap(object sender, System.Windows.Input.GestureEventArgs e)
		{
			// open about page
			NavigationService.Navigate(new Uri("/aboutPage.xaml", UriKind.RelativeOrAbsolute));
		}
    }
}